package org.mall.modules.promotion.controller.admins;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.mall.common.api.vo.CommonResult;
import org.mall.common.api.vo.Result;
import org.mall.common.system.util.JwtUtil;
import org.mall.convert.promotion.admin.ProductRecommendConvert;
import org.mall.modules.pojo.product.bo.ProductSpuBO;
import org.mall.modules.pojo.promotion.admins.AdminsProductRecommendPageVO;
import org.mall.modules.pojo.promotion.admins.AdminsProductRecommendVO;
import org.mall.modules.pojo.promotion.bo.ProductRecommendBO;
import org.mall.modules.pojo.promotion.bo.ProductRecommendPageBO;
import org.mall.modules.pojo.promotion.dto.ProductRecommendAddDTO;
import org.mall.modules.pojo.promotion.dto.ProductRecommendPageDTO;
import org.mall.modules.pojo.promotion.dto.ProductRecommendUpdateDTO;
import org.mall.modules.product.service.ProductSpuService;
import org.mall.modules.promotion.service.ProductRecommendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.mall.common.api.vo.CommonResult.success;

@RestController
@RequestMapping("/admins/product_recommend")
@Api("商品推荐模块")
public class AdminsProductRecommendController {

   @Autowired
    private ProductRecommendService productRecommendService;
   @Autowired
    private ProductSpuService productSpuService;

    @GetMapping("/page")
    @ApiOperation(value = "商品推荐分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memo", value = "备注", example = "1"),
            @ApiImplicitParam(name = "status", value = "状态（0：开启 1 ：关闭）", example = "1"),
            @ApiImplicitParam(name = "type", value = "推荐类型", example = "1"),
            @ApiImplicitParam(name = "pageNo", value = "页码，从 1 开始", example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, example = "10"),
    })
    public Result<?> page(@RequestParam(value = "memo", required = false) String memo,
                          @RequestParam(value = "status", required = false) Integer status,
                          @RequestParam(value = "type", required = false) Integer type,
                          @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        ProductRecommendPageBO result = productRecommendService.getProductRecommendPage(new ProductRecommendPageDTO().setMemo(memo).setStatus(status).setType(type).setPageNo(pageNo).setPageSize(pageSize));
        // 获得商品集合
        List<ProductSpuBO> spus = productSpuService.getProductSpuList(
                result.getList().stream().map(ProductRecommendBO::getProductSpuId).collect(Collectors.toSet()));
        Map<Long, ProductSpuBO> spuMap = spus.stream().collect(Collectors.toMap(ProductSpuBO::getId, account -> account));
        // 拼装结果
        AdminsProductRecommendPageVO response = ProductRecommendConvert.INSTANCE.convert(result);
        response.getList().forEach(recommendVO -> recommendVO.setProductSpuName(spuMap.get(recommendVO.getProductSpuId()).getName()));
        Page<AdminsProductRecommendVO> t=new Page<>();
        t.setCurrent(pageNo).setSize(pageSize).setTotal(response.getTotal())
                .setRecords(response.getList());
        return Result.ok(t);
    }

    @PostMapping("/add")
    @ApiOperation(value = "创建商品推荐")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "推荐类型", required = true, example = "1"),
            @ApiImplicitParam(name = "productSpuId", value = "商品编号", required = true, example = "1"),
            @ApiImplicitParam(name = "sort", value = "排序", required = true, example = "10"),
            @ApiImplicitParam(name = "memo", value = "备注", example = "活动很牛逼"),
    })
    public CommonResult<AdminsProductRecommendVO> add(@RequestBody ProductRecommendAddDTO dto) {
        return success(ProductRecommendConvert.INSTANCE.convert(productRecommendService.addProductRecommend(JwtUtil.getUserIdByToken(), dto)));
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新商品推荐")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品推荐编号", required = true, example = "1"),
            @ApiImplicitParam(name = "type", value = "推荐类型", required = true, example = "1"),
            @ApiImplicitParam(name = "productSpuId", value = "商品编号", required = true, example = "1"),
            @ApiImplicitParam(name = "sort", value = "排序", required = true, example = "10"),
            @ApiImplicitParam(name = "memo", value = "备注", example = "活动很牛逼"),
    })
    public CommonResult<Boolean> update(@RequestBody ProductRecommendUpdateDTO updateDTO) {
        return success(productRecommendService.updateProductRecommend(JwtUtil.getUserIdByToken(), updateDTO));
    }

    @PostMapping("/update_status")
    @ApiOperation(value = "更新商品推荐状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品推荐编号", required = true, example = "1"),
            @ApiImplicitParam(name = "status", value = "状态。1 - 开启；2 - 禁用", required = true, example = "1"),
    })
    public CommonResult<Boolean> updateStatus(@RequestBody ProductRecommendUpdateDTO updateDTO) {
        return success(productRecommendService.updateProductRecommendStatus(JwtUtil.getUserIdByToken(), updateDTO.getId(),updateDTO.getStatus()));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除商品推荐")
    @ApiImplicitParam(name = "id", value = "商品推荐编号", required = true, example = "1")
    public CommonResult<Boolean> delete(@RequestParam("id") Integer id) {
        return success(productRecommendService.deleteProductRecommend(JwtUtil.getUserIdByToken(), id));
    }

    @DeleteMapping("/deleteBatch")
    @ApiOperation(value = "删除商品推荐")
    @ApiImplicitParam(name = "id", value = "商品推荐编号", required = true, example = "1")
    public CommonResult<Boolean> deleteBatch(@RequestParam("ids") String ids) {
        return success(productRecommendService.deleteBatchProductRecommend(JwtUtil.getUserIdByToken(), ids));
    }
}
